//
//  ShoppingCarOrderLCView.m
//  AmountBody
//
//  Created by liuchen on 2017/12/30.
//  Copyright © 2017年 RCMTM. All rights reserved.
//

#import "OrderProcessingViewController.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import <ReactiveObjC/ReactiveObjC.h>
#import <Masonry/Masonry.h>
#import "OrderProcessingActivityView.h"
#import "OrderProcessingInfosView.h"


@interface OrderProcessingViewController()
@property (nonatomic, strong) UIView *container;
@property (nonatomic, strong) OrderProcessingActivityView *activityView;
@property (nonatomic, strong) OrderProcessingInfosView *infosView;
@property (nonatomic, strong) UIView *ordersView;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) NSInteger count;
@end

@implementation OrderProcessingViewController

- (void)setup {

    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    self.container = [[UIView alloc] init];
    [self.view addSubview:_container];
    _container.backgroundColor = [UIColor whiteColor];
    _container.layer.cornerRadius = 10.0f;
    [_container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(60, 60, 60, 60));
    }];

    self.activityView = [[OrderProcessingActivityView alloc] init];
    [_container addSubview:_activityView];

    [_activityView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_container);
        make.top.equalTo(_container).offset(30);
        make.width.height.equalTo(@(350));
    }];

    self.infosView = [[OrderProcessingInfosView alloc] init];
    [_container addSubview:_infosView];
    _infosView.alpha = 0;
    [_infosView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(_container);
        make.top.equalTo(_container).offset(220);
    }];

    @weakify(self)
    _activityView.animationsCompletion = ^{
        @strongify(self)
        [self.infosView startSlideAnimation];
    };
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self startProcessingWithCart:nil];
}

- (void)startProcessingWithCart:(id)cart {

    //请求网络 成功后开始动画
    //loading

    [self.timer invalidate];
    self.timer = nil;
    self.count = 1;
    self.timer = [NSTimer timerWithTimeInterval:1.2 target:self selector:@selector(updateCount) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    [self.timer fire];
}


- (void)updateCount {
    [_activityView animationWithCount:self.count];
    self.count --;
    if (self.count == -1) {
        [self.timer invalidate];
        self.timer = nil;
    }
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = touches.anyObject;
    CGPoint location = [touch locationInView:self.view];
    if (CGRectContainsPoint(self.container.frame, location)) {
        [super touchesEnded:touches withEvent:event];
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (instancetype)init {
    if (self = [super init]) {
        [self setup];
    }
    return self;
}

@end
